<?php

class MsgAction extends CommonAction {

  function Msg() {

		   if($_REQUEST['sub']){

		   	$_POST["cate_sel"]=='0' ? $cate[0] = '':$cate = explode('|',$_POST["cate_sel"]) ;
		   	$_POST["audit"]==''     ? $audit = ''  :$audit = $_POST["audit"] ;
		   	$_POST["mk_top"]==''    ? $mk_top =''  :$mk_top =$_POST['mk_top'];
		   	$_POST["id"]==''        ? $id =''      :$id =$_POST['id'];
		   	 $_REQUEST["username"]==''    ? $username =''  :$username = $_REQUEST['username'];


		   	 $cate_id = $cate[0];

		   	$_SESSION['cate_where']='';
		   	if($cate_id!=''){
		   		$_SESSION['cate_where'].= ' and msg.cate_id = '.$cate_id;
		   	}
		   	if($audit!=''){
		   		$_SESSION['cate_where'].= ' and audit = '.$audit;
		   	}
		   	if($mk_top!=''){
		   		$_SESSION['cate_where'].= ' and mk_top = '.$mk_top;
		   	}
		   	if($id!=''){
		   		$_SESSION['cate_where'].= ' and id = '.$id;
		   	}
		   	if($username!=''){
		   		 $_SESSION['cate_where'].= ' and user.username like "%'.$username.'%"';
		   	}
		   }
		   $_SESSION['order']='id desc';

		   if(!empty($_GET['order'])){

		   	  $_SESSION['order']=$_GET['order'].' desc';
		   }else{

		   	  $_SESSION['order']='id desc';
		   }

           import('@.ORG.Page');

		   $list         =   D('Msg');
		   $count        =   $list->table(array('gdqy_user'=>'user','gdqy_msg_cate'=>'cate','gdqy_msg'=>'msg'))->where('user.user_id=msg.author_id and cate.cate_id=msg.cate_id'.$_SESSION['cate_where'])->count();
		   $page         =   new Page($count,10);
		   $dis_pag      =   $page->show();
           $cate         =   $list->list_msg();
           $list_msg         =  $cate->table(array('gdqy_user'=>'user','gdqy_msg_cate'=>'cate','gdqy_msg'=>'msg'))->where('user.user_id=msg.author_id  and cate.cate_id=msg.cate_id'.$_SESSION['cate_where'])->order($_SESSION['order'])->limit($page->firstRow.','.$page->listRows)->select();
           $this->assign('page',$dis_pag);
           $this->assign('list_msg',$list_msg);

		   $list_c        =   D('MsgCate');
           $cate         =   $list_c->list_cate();
           $list_cate=  $cate->field('cate_id,cate_name,parent_id,bpath,concat(bpath,"-",cate_id) as path')->order('path')->select();

            foreach ($list_cate as $key=>$val){
            	$list_cate[$key]['count']=count(explode('-',$val['bpath']));
            }
           $this->assign('list_cate',$list_cate);
           $this->display();

    }

  function del_msg(){
	       if($_GET['id']){
	       	 $id         =   $_GET['id'];
	       	 $list       =   D('Msg');
	       	 $effe_row   =   $list->where('id = "'.$id.'"')->delete();
	       	 $effe_ro   =   $list->where('mes_id = "'.$id.'"')->delete();
	       	   if($effe_row){
	       	   	header('Location:Msg');
	       	   }else{
	       	   	echo '<script type="text/javascript">alert("false!error");location.href="Msg"</script>';
	       	   }

	       }
  }

  function mktop_msg(){

	  	  if($_GET['id']){
	        $list       =   D('Msg');
	        $data['id'] =   $_GET['id'];
	        $data['mk_top'] =   $_GET['t'];
	        $list->data($data)->save();
	  	  }
	     	echo '<script type="text/javascript">alert("success");location.href="Msg"</script>';
  }

  function audit_msg(){

	  	  if($_GET['id']){
	        $list       =   D('Msg');
	        $data['id'] =   $_GET['id'];
	        $data['audit'] =   $_GET['t'];
	        $list->data($data)->save();
	  	  }
	     	echo '<script type="text/javascript">alert("success");location.href="Msg"</script>';
  }

  function check(){

	       if(!empty($_POST['checkbox'])){

	            $list       =   D('Msg');

		      	if($_POST['all_del_sub_x']){
		      		for($i=0;$i<count($_POST['checkbox']);$i++){

		          	    $list->where('id ='.$_POST['checkbox'][$i])->delete();
		          }
		         echo '<script type="text/javascript">alert("delete is success");location.href="Msg"</script>';

		  	   }

		  	   if($_POST['all_top_sub']){

		          for($i=0;$i<count($_POST['checkbox']);$i++){

	                    $date['id']       = $_POST['checkbox'][$i];
	                    $date['mk_top']  = $_POST['all_top'];
		          	    $list->data($date)->save();
		          }
		         echo '<script type="text/javascript">alert("mk_top is success");location.href="Msg"</script>';
		  	   }

		  	   if($_POST['all_audit_sub']){

		  	   	  for($i=0;$i<count($_POST['checkbox']);$i++){

	                    $date['id']       = $_POST['checkbox'][$i];
	                    $date['audit']  = $_POST['all_audit'];
		          	    $list->data($date)->save();
		          }
		         echo '<script type="text/javascript">alert("audit is success");location.href="Msg"</script>';

		  	   }
	       }else{

	       	echo '<script type="text/javascript">alert("no checked");location.href="Msg"</script>';
	       }
  }

  function comm_detail(){

	  	$list       =   D('Msg');

	  	$list_comm =   $list->relation(true)->where('id ='.$_GET['id'])->select();

	  /*	array(1) {
  [0] => array(14) {
    ["id"] => string(2) "94"
    ["cate_id"] => string(1) "6"
    ["author_id"] => string(1) "2"
    ["content"] => string(0) ""
    ["images"] => string(5) "1.png"
    ["audit"] => string(1) "1"
    ["mk_top"] => string(1) "0"
    ["comm_count"] => string(1) "0"
    ["time"] => string(8) "32432423"
    ["username"] => string(10) "用户名2"
    ["create_time"] => string(9) "123445322"
    ["get"] => string(3) "324"
    ["login_ip"] => string(12) "123.34.321.3"
    ["Comm"] => array(4) {
      [0] => array(3) {
        ["author"] => string(6) "zuozhe"
        ["content"] => string(6) "内容"
        ["time"] => string(7) "1244434"
      }
      [1] => array(3) {
        ["author"] => string(6) "zuozhe"
        ["content"] => string(0) ""
        ["time"] => string(7) "1244434"
      }
      [2] => array(3) {
        ["author"] => string(6) "zuozhe"
        ["content"] => string(0) ""
        ["time"] => string(7) "1244434"
      }
      [3] => array(3) {
        ["author"] => string(6) "zuozhe"
        ["content"] => string(0) ""
        ["time"] => string(7) "1244434"
      }
    }
  }
}*/
   	 foreach($list_comm[0] as $key=>$val){

	 	 $msg[$key] = $val;
	 }

	 foreach($list_comm[0]["Comm"] as $key=>$val){

	 	 $comm[] = $val;
	 }



	  	$this->assign('comm',$comm);
	  	$this->assign('msg',$msg);

	  	$this->display();

  }
}
?>